package memberrewards.service;

import hackathon1212.ServletPojoAdapter;

import java.text.NumberFormat;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import memberrewards.delegate.RewardsDelegate;
import memberrewards.dtos.AccountDetailDTO;
import memberrewards.dtos.TierConfigDTO;

/**
 * Servlet implementation class PointsToCash
 */
public class PointsToCash extends ServletPojoAdapter {
	private static final long serialVersionUID = -460084643178303368L;
	
	protected Map<String,Object> getBusinessMethod(Map<String, String[]> params){
		//Query Params
		final String pan =params.get("pan")!=null? params.get("pan")[0]:null;
		final String pointsVal =params.get("pointsval")!=null? params.get("pointsval")[0]:null;
		
		//Default response
		final Map<String,Object> ret = new HashMap<String, Object>();
		ret.put("status", "Card Not Found");	
		ret.put("cash", "0");
		final AccountDetailDTO accountDetail = RewardsDelegate.getAccountDetail(pan);
		if (accountDetail!=null) {
			final TierConfigDTO tierDetail = accountDetail.getTierConfig();
			final Double cpp = new Double(tierDetail.getCostPerPoint());
			try {
				final Double points = new Double(pointsVal);
				final Double cashVal = points * cpp;
				System.out.println("Cash Value:- >"+cashVal);
				NumberFormat nf=NumberFormat.getNumberInstance(Locale.ENGLISH);	
				nf.setMaximumFractionDigits(2);
				ret.put("status", "Success");	
				ret.put("cash", nf.format(cashVal));
			} catch (Exception ex) {
				ret.put("status", "Invalid Points Amount");
			}
		}
		return ret;		
	}
//	protected Map<String,String> postBusinessMethod(Map<String, String[]> params){
//		final Map<String,String> ret = new HashMap<String, String>();
//		ret.put("status", "Success");	
//		ret.put("cash", "100");
//		return ret;		
//	}

}
